package com.sky.controller.admin;


import com.sky.properties.AliOssProperties;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RequestMapping("/admin/common")
@RestController
@Slf4j
public class UploadController {

    @Autowired
    AliOssProperties aliOssProperties;




    @PostMapping("upload")
    public Result<String> imageUpload(@RequestParam(name = "file") MultipartFile imageFile) throws IOException {
        AliOssUtil aliOssUtil=new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());
        //获取文件后缀名
        String originalName=imageFile.getOriginalFilename();
        String houzhui = originalName.substring(originalName.lastIndexOf('.'));

        String newFileName= UUID.randomUUID().toString();

        String fileName=newFileName+houzhui;

        String url=aliOssUtil.upload(imageFile.getBytes(),fileName);
        log.info("正在上传{}处的文件到云端",imageFile.getOriginalFilename());
        return Result.success(url);

    }
}
